package org.quartz.example002;


import static org.quartz.DateBuilder.futureDate;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.JobKey.jobKey;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.util.Date;

import org.junit.Test;
import org.quartz.DateBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.SimpleTrigger;
import org.quartz.DateBuilder.IntervalUnit;
import org.quartz.examples.example2.SimpleJob;
import org.quartz.impl.StdSchedulerFactory;

public class Example002_1 {

	JobDetail job  = null;
	SimpleTrigger trigger = null;
	Date ft = null;
	Date startTime = null;
	
	@Test
	public void test() throws SchedulerException {
		 SchedulerFactory sf = new StdSchedulerFactory();
	     Scheduler sched = sf.getScheduler();
	     
	     startTime = DateBuilder.nextGivenSecondDate(null, 1);

	     job = newJob(TestJob.class).withIdentity("job1", "group1").build();
	        
	     trigger = (SimpleTrigger)newTrigger().withIdentity("trigger1", "group1")
	    		 .withSchedule(simpleSchedule()
		                    .withIntervalInSeconds(1)
		                    .repeatForever()).startAt(startTime).build();
	     
	     ft = sched.scheduleJob(job, trigger);
	     
	     sched.start();
	     try {
				Thread.sleep(25L * 1000L); 
			} catch (Exception e) {
			}
	     
	}
	
	@Test
	public void test1() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 15);
		
		job = newJob(TestJob.class).withIdentity("job2", "group1").build();

	    trigger = (SimpleTrigger) newTrigger().withIdentity("trigger2", "group1").startAt(startTime).build();

	    ft = sched.scheduleJob(job, trigger);
	   
		sched.start();
		
		
	}
	
	
	@Test
	public void test2() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 15);
		
		 job = newJob(TestJob.class)
		       .withIdentity("job3", "group1")
		       .build();

		 trigger = newTrigger()
		           .withIdentity("trigger3", "group1")
		           .startAt(startTime)
		           .withSchedule(simpleSchedule()
		           .withIntervalInSeconds(10)
		           .withRepeatCount(10))
		           .build();

	    ft = sched.scheduleJob(job, trigger);
		
		sched.start();
	}
	
	
	@Test
	public void test3() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 15);
		
		job = newJob(TestJob.class)
	            .withIdentity("job4", "group1")
	            .build();

	    trigger = newTrigger()
	            .withIdentity("trigger4", "group1")
	            .startAt(startTime)
	            .withSchedule(simpleSchedule()
	                    .withIntervalInSeconds(10)
	                    .withRepeatCount(5))
	            .build();

	        ft = sched.scheduleJob(job, trigger);
		
		sched.start();
	}
	
	
	
	@Test
	public void test4() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 15);
		
		job = newJob(TestJob.class)
	            .withIdentity("job5", "group1")
	            .build();

	        trigger = (SimpleTrigger) newTrigger() 
	            .withIdentity("trigger5", "group1")
	            .startAt(futureDate(5, IntervalUnit.MINUTE))
	            .build();

	        ft = sched.scheduleJob(job, trigger);
		
		sched.start();
		
		try {
            // wait five minutes to show jobs
            Thread.sleep(10L * 1000L); 
            // executing...
        } catch (Exception e) {
        }
		
		
		sched.shutdown(true);
	}
	
	
	
	@Test
	public void test5() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 15);
		
		job = newJob(TestJob.class)
	            .withIdentity("job5", "group1")
	            .build();

	        trigger = (SimpleTrigger) newTrigger() 
	            .withIdentity("trigger5", "group1")
	            .startAt(futureDate(5, IntervalUnit.MINUTE))
	            .build();

	        ft = sched.scheduleJob(job, trigger);
		
		sched.start();
		
		try {
			// wait five minutes to show jobs
			Thread.sleep(10L * 1000L); 
			// executing...
		} catch (Exception e) {
		}
		
		
		sched.shutdown(true);
	}
	
	@Test
	public void test6() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 5);
		
		 job = newJob(TestJob.class)
		            .withIdentity("job6", "group1")
		            .build();

		        trigger = newTrigger()
		            .withIdentity("trigger6", "group1")
		            .startAt(startTime)
		            .withSchedule(simpleSchedule()
		                    .withIntervalInSeconds(40)
		                    .repeatForever())
		            .build();
		            
		        ft = sched.scheduleJob(job, trigger);
		
		sched.start();
		
		try {
			Thread.sleep(10L * 1000L); 
		} catch (Exception e) {
		}
		
		
		sched.shutdown(true);
	}
	
	
	@Test
	public void test7() throws SchedulerException {
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 5);
		
		job = newJob(TestJob.class)
	            .withIdentity("job7", "group1")
	            .build();
	        
	        trigger = newTrigger()
	            .withIdentity("trigger7", "group1")
	            .startAt(startTime)
	            .withSchedule(simpleSchedule()
	                    .withIntervalInMinutes(5)
	                    .withRepeatCount(20))
	            .build();

	        ft = sched.scheduleJob(job, trigger);
		
		sched.start();
		
		try {
			Thread.sleep(10L * 1000L); 
		} catch (Exception e) {
		}
		sched.shutdown(true);
	}
	
	
	
	@Test
	public void test8() throws SchedulerException {
		
		SchedulerFactory sf = new StdSchedulerFactory();
		
		Scheduler sched = sf.getScheduler();
		
		startTime = DateBuilder.nextGivenSecondDate(null, 5);
		
		sched.start();
		
		job = newJob(TestJob.class).withIdentity("job7", "group1").build();
		        
        trigger = newTrigger()
            .withIdentity("trigger7", "group1")
            .startAt(startTime)
            .withSchedule(
            	simpleSchedule().withIntervalInSeconds(5).withRepeatCount(15)
            )
            .build();

        ft = sched.scheduleJob(job, trigger);
        
        job = newJob(TestJob.class)
            .withIdentity("job8", "group1")
            .storeDurably()
            .build();
		    
        sched.addJob(job, true);
        
        sched.triggerJob(jobKey("job8", "group1"));

        try {
            Thread.sleep(30L * 1000L); 
        } catch (Exception e) {
        }

        trigger = newTrigger()
	        .withIdentity("trigger7", "group1")
	        .startAt(startTime)
	        .withSchedule(
	        		simpleSchedule().withIntervalInSeconds(5).withRepeatCount(20)
	         )
	        .build();

		ft = sched.rescheduleJob(trigger.getKey(), trigger);
		        
        try {
            Thread.sleep(300L * 1000L); 
        } catch (Exception e) {
        }

		sched.shutdown(true);

		SchedulerMetaData metaData = sched.getMetaData();
		
		System.out.println("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
	}

	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
